<?php
$act = $_REQUEST['act'];


include "../../../lib/DB.php";
$db = new DB();
$table = 'goods';
$data = $_GET;
if($act == 'add')
{
    $data = $_REQUEST;
    if (file_exists("/upload/" . $_FILES["goods_url"]["name"]))
    {
        echo $_FILES["goods_url"]["name"] . " 文件已经存在。 ";
    }
    else
    {
        move_uploaded_file($_FILES["goods_url"]["tmp_name"], "upload/" . $_FILES["goods_url"]["name"]);
        $url =  "upload/" . $_FILES["goods_url"]["name"];
    }

    $data['goods_url'] = $url;

    unset($data['act'],$data['id'],$data['change'],$data['act']);
    $id = $db->table($table)->insert($data);
    echo "<script >alert('添加成功..');location.href='goods.php'</script>";
    exit;
}
if($act == 'edit')
{

    if (file_exists("/upload/" . $_FILES["goods_url"]["name"]))
    {
        echo $_FILES["goods_url"]["name"] . " 文件已经存在。 ";
    }
    else
    {
        move_uploaded_file($_FILES["goods_url"]["tmp_name"], "upload/" . $_FILES["goods_url"]["name"]);
        $url =  "upload/" . $_FILES["goods_url"]["name"];
    }

    $id = $_POST['gid'];
    $data = $_POST;
    unset($data['act'],$data['gid'],$data['change']);
    $data['goods_url'] = $url;
    $db = $db->table($table)->where(['gid' => $id])->update($data);

    echo "<script >alert('修改成功..');location.href='goods.php'</script>";
    exit;
}
if($act == 'del')
{
    $id = $_GET['gid'];
    $db->table($table)->where(['gid' => $id])->delete();
    echo "<script >alert('删除成功..');location.href='goods.php'</script>";
    exit;

}